package cc.tuhaolicai.mt.user.mobile.config;

import cc.tuhaolicai.mt.user.mobile.json.ProtoolsJsonFactory;
import com.jfinal.config.*;
import com.jfinal.ext.handler.UrlSkipHandler;
import com.jfinal.kit.PropKit;
import com.jfinal.log.Log4jLogFactory;
import com.jfinal.render.ViewType;
import com.jfinal.template.Engine;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import pro.tools.constant.StrConst;
import pro.tools.data.decimal.Decimal;
import pro.tools.system.ToolSystem;

import java.math.MathContext;
import java.math.RoundingMode;

/**
 * @author SeanDragon Create By 2017-04-26 16:08
 */
public class DefaultConfig extends JFinalConfig {

    private static final Logger log = LoggerFactory.getLogger(DefaultConfig.class);

    /**
     * 配置常量
     *
     * @param constants
     */
    @Override
    public void configConstant(Constants constants) {

        log.info("configConstant 缓存 properties");
        PropKit.use("init.properties");

        //加载微服务信息
//        PropKit.use("service/initService.properties");

        log.info("configConstant 设置日期格式");
        constants.setJsonDatePattern("yyyy-MM-dd HH:mm:ss");

        log.info("configConstant 设置字符集");
        constants.setEncoding(StrConst.DEFAULT_CHARSET_NAME);

        log.info("configConstant 设置是否开发模式");
        constants.setDevMode(PropKit.getBoolean("config.devMode", true));

        log.info("configConstant 设置视图模式");
        constants.setViewType(ViewType.JSP);
        constants.setViewExtension(".jsp");//默认为页面添加后缀名

        log.info("configConstant 设置json工厂类");
        constants.setJsonFactory(ProtoolsJsonFactory.me());
        //constants.setJsonFactory(JFinalJsonFactory.me()); // 默认无需设置
        //constants.setJsonFactory(FastJsonFactory.me()); // FastJson
        //constants.setJsonFactory(JacksonFactory.me()); // Jackson

        log.info("configConstant 设置log工厂类");
        constants.setLogFactory(new Log4jLogFactory()); // 参考Log4jLogFactory、Log4jLog实现LogBackFactory、LogBackLog

        log.info("configConstant 设置path相关");
        String filePath = (ToolSystem.isWindows() ? "c:" : "") + StrConst.FILE_SEP + "images" + StrConst.FILE_SEP;
        constants.setBaseUploadPath(filePath); // 上传公共路径 + File.separator + "files"
        constants.setBaseDownloadPath(filePath); // 下载公共路径 + File.separator + "files"

        log.info("configConstant 视图error page设置");
        constants.setError404View("/404.html");
        constants.setError500View("/500.jsp");
    }

    /**
     * 配置路由
     *
     * @param routes
     */
    @Override
    public void configRoute(Routes routes) {
        log.info("configRoute 注解注册路由");
        routes.add(new RoutesScan());//添加其他扫描注入路由
    }

    /**
     * 配置模板引擎
     *
     * @param engine
     */
    @Override
    public void configEngine(Engine engine) {
    }

    /**
     * 配置插件
     *
     * @param plugins
     */
    @Override
    public void configPlugin(Plugins plugins) {
        ConfigPlugin.config(plugins);
    }

    /**
     * 配置拦截器
     *
     * @param interceptors
     */
    @Override
    public void configInterceptor(Interceptors interceptors) {

    }

    @Override
    public void configHandler(Handlers handlers) {
        log.info("configHandler 全局配置处理器，设置跳过哪些URL不处理");
        handlers.add(new UrlSkipHandler("/fonts/.*|/im/.*|.*.htm|" +
                ".*.html|.*.js|.*.css|" +
                ".*.json|.*.png|.*.gif|.*.jpg|.*.jpeg|.*.bmp|.*.ico|" +
                ".*.exe|.*.txt|.*.zip|.*.rar|.*.7z|" +
                "/404.jsp|/404.html|" +
                "/com/.*|/css/.*|/images/.*|/js/.*", false));

        log.info("configHandler 全局配置处理器，记录日志和request域值处理");
    }

    /**
     * 系统启动完成后执行
     */
    @Override
    public void afterJFinalStart() {

        log.info("afterJFinalStart 初始化系统配置信息");
//        initSysConfig();

//        LoanInit.start();
        Decimal.setDefaultMathContext(new MathContext(32, RoundingMode.HALF_UP));

    }

    /**
     * 系统关闭前调用
     */
    @Override
    public void beforeJFinalStop() {

        log.info("beforeJFinalStop 释放系统配置信息");
//        stopSysConfig();

        log.info("befreJFinalStop 释放ToolHttpSender连接池");
//        ToolHttpSender.shutdown();
    }
}
